let app = getApp()

Page({
  data: {
		school: '',
		major: '',
		editIndex: -1,
		degreeOptions: ['小学','初中','高中','中专','大专', '本科', '硕士', '博士'],
		degreeIndex:1,
		currentYear: new Date().getFullYear(), // 当前年份
		years: [],                             // 所有可选年份数组
		yearRange: [[], []],                   // picker的二维数组
		pickerValue: [0, 0],                   // picker选中索引
		startYear: 2020,                       // 选择的开始年份
		endYear: 2024,                          // 选择的结束年份
		del_nocancel: false
	},

	onLoad(options) {
		this.setData({
			editIndex: options.index || -1
		})
		
		// 生成年份数组（最近50年）
		const years = []
		for (let i = this.data.currentYear; i >= this.data.currentYear - 50; i--) {
		  years.push(i)
		}
		this.setData({
		  years,
		  yearRange: [years, years] // 初始化两列相同数据
		})
		
		if(this.data.editIndex > 0) {
			this.requests()
		}
		
	},
	
	requests:function(){
	  var that = this
	  var key = wx.getStorageSync('key')
	  var uid = wx.getStorageSync('u')
	  var id = that.data.editIndex
	  wx.request({
	    url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=getEducationInfo&typ=json',
	    method: 'GET',
	    data: { k: key, u: uid, id: id },
	    header: {
	      'content-type': 'application/json'
	    },
	    success: function (res) {
	      if (res.data.status == 200) {
				that.setData({
					school: res.data.data.school,
					major: res.data.data.major,
					degreeIndex: res.data.data.degree,
					startYear: res.data.data.startYear,
					endYear: res.data.data.endYear              
				})
	      } else {
	        that.show('数据加载失败')
	      }
	
	    }
	  })
	},
    // 列变化时处理联动
    bindColumnChange(e) {
      const { column, value } = e.detail
      const { years, pickerValue } = this.data
  
      // 如果是第一列（开始年份）变化
      if (column === 0) {
        // 结束年份不能小于开始年份
        const startYear = years[value]
        const endYear = years[pickerValue[1]]
        
        if (endYear < startYear) {
          // 自动调整结束年份为开始年份
          const newPickerValue = [value, value]
          this.setData({
            pickerValue: newPickerValue,
            startYear,
            endYear: startYear
          })
        }
      }
    },
	// 最终选择确认
	  bindPickerChange(e) {
	    const [startIdx, endIdx] = e.detail.value
	    const startYear = this.data.years[startIdx]
	    const endYear = this.data.years[endIdx]
	
	    this.setData({
	      startYear,
	      endYear: Math.max(startYear, endYear) // 确保结束>=开始
	    })
	  },
	// 选择变化
	  degreeChange(e) {
		this.setData({ degreeIndex: e.detail.value[0] })
	  },
	  // 输入事件处理
	   handleSchool(e) {
	     this.setData({
	       school: e.detail.value // 实时更新到data
	     })
	   },
	   handleMajor(e) {
	     this.setData({
	       major: e.detail.value // 实时更新到data
	     })
	   },
	   
	saveInfo() {
		const that = this
		const key = wx.getStorageSync('key')
		const uid = wx.getStorageSync('u')
		const id = that.data.editIndex
		if(that.data.school == ''){
			that.show('请输入学校名称')
		} else if(that.data.major == '') {
			that.show('请输入所学专业')
		} else {
			const educationInfo = {
				school: that.data.school,
				major: that.data.major,
				degree: that.data.degreeIndex,
				startYear: that.data.startYear,
				endYear: that.data.endYear
			}
			const parme = {
				k: key, 
				u: uid,
				id: id,
				education_info: JSON.stringify(educationInfo),
			};
			wx.request({
				url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=addResumeEducationInfo&typ=json',
				data: parme,
				method: 'POST', 
				header: {
					"Content-Type": "application/x-www-form-urlencoded"
				}, // 设置请求的 header
				success: function (res) {
					if(res.data.status == 200){
						app.show('保存成功')
						wx.redirectTo({
							url: '../personal_resume/personal_resume',
						})
					}else{
						app.show('保存失败')
					}
				},
				fail: function (res) {
				  
				}
			})
		}
	},
	//删除
	delete() {
	  this.setData({
	    del_nocancel: true
	  })
	},
	del_confirm() {
	  this.del()
	},
	del_cancel() {
	  this.setData({
	    del_nocancel: false
	  })
	},
	del() {
		const that = this
		const key = wx.getStorageSync('key')
		const uid = wx.getStorageSync('u')
		const id = that.data.editIndex
		const parme = {
			k: key, 
			u: uid,
			id: id
		};
		wx.request({
			url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=delResumeEducationInfo&typ=json',
			data: parme,
			method: 'POST', 
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			}, // 设置请求的 header
			success: function (res) {
				if(res.data.status == 200){
					app.show('删除成功')
					wx.redirectTo({
						url: '../personal_resume/personal_resume',
					})
				}else{
					app.show('删除失败')
				}
			},
			fail: function (res) {
			  
			}
		})
	}
})